package com.kancy.vip.pattern.proxy.dynamicproxy;

import com.kancy.vip.pattern.proxy.Girl;
import com.kancy.vip.pattern.proxy.Love;
import com.kancy.vip.pattern.proxy.Person;
import com.kancy.vip.pattern.proxy.staticproxy.Boy;
import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.*;

import java.lang.reflect.Method;

/**
 * @author kancy
 * @date 2019/3/16 17:00
 */
public class CglibDynamicProxy {

	public static void main(String[] args) {

		System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\class");

		Girl target = new Girl("pmm",23, 170);
		Love love = (Love) Proxy.newProxyInstance(Girl.class.getClassLoader()
				, new Class[]{Love.class}, new InvocationHandler() {
					@Override
					public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
						System.out.println(method);
						return method.invoke(target, objects);
					}
				});

		Boy boy = new Boy("kancy", 24, 170);
		love.toString();
		love.hashCode();
		love.findLove(boy);

		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(Girl.class);
		enhancer.setCallback(new MethodInterceptor() {
			@Override
			public Object intercept(Object proxy, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
				System.out.println(method);
				return method.invoke(target, objects);
			}
		});
		Girl girl = (Girl) enhancer.create();

		girl.findLove(boy);
		girl.toString();
		girl.getAge();

	}
}
